//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class IconSetFormattingRule : ConditionalFormattingRule
    {
        internal override ConditionalFormattingType Type
        {
            get { return ConditionalFormattingType.IconSet; }
        }
        internal override bool Apply(Cell cell)
        {
            object value = cell.Value;
            if (value is double)
            {
                var d = (double)value;
                cell.Icon = new IconSetInformation(d, this.minValue, this.maxValue,false,  IconSet);
            }
            return true;
        }
        internal override void ApplyRange(Range range)
        {
            maxValue = double.MinValue;
            minValue = double.MaxValue;
            foreach (Cell cell in range.GetNonEmptyCells())
            {
                object value = cell.Value;
                if (value is double)
                {
                    var d = (double) value;
                    maxValue = Math.Max(maxValue, d);
                    minValue = Math.Min(minValue, d);
                }
            }
            deltaValue = maxValue - minValue;
        }
        private double maxValue;
        private double minValue;
        private double deltaValue;

        internal override void ReadInner(ReadContext c)
        {
            IconSet = c.ReadOptionalElement<IconSet>("iconSet");

        }
        internal override ConditionalFormattingRule Clone()
        {
            throw new System.NotImplementedException();
        }
        internal override void WriteInner(WriteContext c)
        {
            c.WriteOptionalElement(IconSet);
        }
        private IconSet iconSet;

        internal IconSetFormattingRule(IconSet value)
        {
            this.iconSet = value;
        }

        public IconSetFormattingRule()
        {
        }

        public IconSet IconSet
        {
            get { return iconSet; }
            set { iconSet = value; }
        }

    }
}
